package com.dex4d.ui.wallet.model;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
import android.content.Context;

import com.dex4d.model.User;
import com.google.gson.annotations.SerializedName;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 作者    LiQingyi
 * 时间    2018/11/26 11:16
 * 文件    dex4d-android
 * 描述    本地数据储存钱包交易记录
 */

@Entity
public class WalletTx {

    @Ignore
    public int position;

    @PrimaryKey(autoGenerate = true)
    public long id;

    public String symbol;//货币符号

    /**
     * 转入 1
     * 转出 0
     */
    public int cashflow;//现金流转
    public String nonce;//账户发生交易的次数
    public String from;
    public String to;//调用合约时这个字段是合约地址，普通交易时为目标用户地址
    @SerializedName("gaslimit")
    public String gasLimit;//支付该笔交易最大数量的 gas
    @SerializedName("gasprice")
    public String gasPrice;//支付该笔交易每个单元 gas 的价格
    public String data;//交易携带的信息
    public String value;//转账数量。在这里我们的目的是调用投票合约，故赋值为 0
    public long txTimeStamp;//交易发起时间
    public String balance;//账户余额
    @SerializedName("txhash")
    public String txHash;//交易Hash
    public String blockHeight;

    @SerializedName("gasused")
    public String gasUsed;

    public String txFee;//交易费用

    /**
     * 1:COMPLETED(Success)
     * 2:Pending(PENDING)
     * 3:Success(ERROR)
     * 4:Fail(FAILED)
     * 5:UNKNOWN
     * 6:DELETED
     */
    @SerializedName("status")
    public int txReceiptStatus;//交易状态


    public WalletTx() {

        this.txTimeStamp = Calendar.getInstance().getTimeInMillis();
        this.from = User.getInstance().getAddress();
        this.txReceiptStatus = 2;
    }

    public static WalletTx createOut() {
        WalletTx tx = new WalletTx();
        tx.cashflow = 0;
        return tx;
    }

    public static WalletTx createIn() {
        WalletTx tx = new WalletTx();
        tx.cashflow = 0;
        return tx;
    }

    public String getTxTimeStamp() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(txTimeStamp);
        return sdf.format(calendar.getTime());
    }

    public String getTxReceiptStatus(Context context) {
        int resId = context.getResources().getIdentifier("wallet_tx_item_status_" + txReceiptStatus, "string", context.getPackageName());
        return context.getResources().getString(resId);
    }

    public String getWeb2Url(){
        return "https://rinkeby.etherscan.io/tx/"+txHash;
    }
}
